package sk.tuke.homescreen;

import sk.tuke.android4all.controls.CustomFragment;
import sk.tuke.android4all.listeners.SwipeListener;
import sk.tuke.android4all.services.Tts;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class QuickPickFragment extends CustomFragment{

	private static String TAG = "QuickPickFragment";
	private Context mContext = null;
	private Activity mActivity = null;
	private Tts mSpeechEngine = null;
	private GeneralContext mGeneralContext = null;
	
	
	public QuickPickFragment(GeneralContext aContextObj_in){
		this.mActivity = aContextObj_in.getActivity();
		this.mContext = mActivity.getApplicationContext();
		mSpeechEngine = aContextObj_in.getSpeechEngine();
		mGeneralContext = aContextObj_in;
	}
	
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_quickpick, container, false);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);        
        this.getView().setOnTouchListener(new SwipeListener(mContext){

			@Override
			public void onDoubleTapAction(int touchCount) {
				Log.d(TAG, "Touches detected: " + touchCount);
				Vibrator mVibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
				String appTobeLaunched = new QuickPickAssignments(mContext).getAssignment(touchCount);
				Log.d(TAG, "App to be launched returned " + appTobeLaunched);
				if(appTobeLaunched.equals("")){
					mSpeechEngine.sayMessage(getResources().getString(R.string.quickpick_app_not_found));
				}else{
					mActivity.getFragmentManager().beginTransaction().replace(R.id.container, new MenuFragment()).commit();
					Intent mIntent = mContext.getPackageManager().getLaunchIntentForPackage(appTobeLaunched);
	                mIntent.addCategory(Intent.CATEGORY_LAUNCHER);
	                mIntent.putExtra("APP_NAME", appTobeLaunched);
	                mVibrator.vibrate(500);
	                startActivity(mIntent);		
				}
			}

			@Override
			public void onZoomInAction() {
				Log.i(TAG, "OnZoomInAction");
				mSpeechEngine.sayMessage(mContext.getResources().getString(R.string.voice_quickpick_closed));
				mActivity.getFragmentManager().beginTransaction().replace(R.id.container, new MenuFragment()).commit();		
			}

			@Override
			public void onZoomOutAction() {
				// TODO Auto-generated method stub
				
			}
        });
    }

	@Override
	public void returnAction() {
		Log.i(TAG, "returnAction");
		if(getFragmentManager() != null){		
			getFragmentManager().beginTransaction().replace(R.id.container, new MenuFragment()).commit();	
		}
	}
    
}
